#coding=utf-8
import requests

import numpy as np
import pandas as pd
import re
import urllib2
import urllib


def downloade_page(url):
    url=url.strip()
    header={'User-Agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64;rv:54.0)Gecko/20100101 Firefox/54.0'}
    html=requests.get(url,headers=header).content

    return html
#获取歌词链接
def getLink(html,name):
    pattern=re.compile('<a href="(.*?)".*?</a>',re.S)
    song_url=re.findall(pattern,html)


    list_songLink=[]
    i=0

    for url in song_url:
        url=getUrl(url)
        #去除非歌词页，网址特为非艺人名字
        if(url[27:27+len(name)]==name):
            list_songLink.append(url)

            i+1

    return list_songLink

def songList(listSong,html,sun,num):
    pattern=re.compile('<div class="album".*?</div>(.*?)<script.*?>',re.S)
    res=re.findall(pattern,html)
    rawTitle=""
    for item in res:
        rawTitle=rawTitle+item





    de_pattern=re.compile('<a.*?>(.*?)</a>',re.S)
    songTitle=re.findall(de_pattern,rawTitle)


    for item in songTitle:
        if item=='':
            songTitle.remove(item)


    for i in range(0,len(listSong)):
        sun['songLink'].loc[num]=listSong[i]
        sun['songName'].loc[num]=songTitle[i]
        num=num+1



    return sun,num

def main():
    num=0

    #创建表格

    #输入艺人
    name=raw_input("输入艺人英文名字")
    #输入艺人主页网址
    baseUrl=raw_input("输入艺人主页 ")
    response=downloade_page(baseUrl)
    song=getLink(response,name)
    sun = pd.DataFrame(np.zeros((len(song), 3)), columns=['songName', 'songLink', 'songLyrics'])
    sun,num=songList(song,response,sun,num)
    #给exel文件起名
    sun.to_csv(name+'.csv')
    print sun
    return sun

def getUrl(baseUrl):
        stripUrl = baseUrl.strip()
        stripUrl=stripUrl[2:]

        url = "http://azlyrics.com"+stripUrl
        return url

if __name__ == '__main__':
    main()






